package com.scau402.system1.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scau402.system1.domain.factory.WmsOutboundOrderFactory;
import com.scau402.system1.domain.po.WmsAdjustBoundOrderDetailPO;
import com.scau402.system1.domain.po.WmsAuditOrderPO;
import com.scau402.system1.domain.po.WmsOutboundOrderDO;
import com.scau402.system1.domain.po.WmsOutboundOrderDetailDO;
import com.scau402.system1.domain.vo.WmsOutboundOrderAndDetailVO;
import com.scau402.system1.domain.vo.WmsOutboundOrderDetailVO;
import com.scau402.system1.domain.vo.WmsOutboundOrderVO;
import com.scau402.system1.mapper.IWmsOutboundOrderDetailMapper;
import com.scau402.system1.mapper.IWmsOutboundOrderMapper;
import com.scau402.system1.repository.IWmsOutboundOrderRepository;
import com.scau402.system1.service.IWmsOutboundOrderDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 出库模块Repo实现类
 * @since 2024/2/12
 */
@Repository
@Slf4j
public class WmsOutboundOrderRepositoryImpl implements IWmsOutboundOrderRepository {
    @Autowired
    private IWmsOutboundOrderMapper outboundOrderMapper;
    @Autowired
    private IWmsOutboundOrderDetailMapper outboundOrderDetailMapper;
    @Autowired
    private WmsOutboundOrderFactory outboundOrderFactory;
    @Autowired
    private IWmsOutboundOrderDetailService outboundOrderDetailService;

    /**
     * 根据审核单的id列表获取出库单列表
     * @param poList 审核单PO列表
     * @return
     */
    @Override
    public List<WmsAdjustBoundOrderDetailPO> findOutboundList(List<WmsAuditOrderPO> poList) {
        if (poList.isEmpty()) {
            return new ArrayList<>();
        }
        return null;
    }

    /**
     * 根据审核单id查找对应的出库主单和详情单
     * @param po 审核单PO
     * @return 出库单主单和详情单
     * @since 2024/2/12
     */
    @Override
    public WmsOutboundOrderAndDetailVO findOutboundByAudit(WmsAuditOrderPO po) {
        if (po == null||po.getId()==null) {
            return null;
        }
        // 获取出库主单
        WmsOutboundOrderDO outboundDO = outboundOrderMapper.selectOne(new QueryWrapper<WmsOutboundOrderDO>().eq("audit_id", po.getId()));
        String orderCode = outboundDO.getOrderCode();
//        List<WmsOutboundOrderDetailDO> detailList = outboundOrderDetailMapper.selectList(new QueryWrapper<WmsOutboundOrderDetailDO>().eq("outbound_order_code", orderCode));

        // 转换为VO对象
        WmsOutboundOrderVO outboundOrderVO = outboundOrderFactory.toWmsOutboundOrderVo(outboundDO);
//        List<WmsOutboundOrderDetailVO> detailVOList = detailList.stream().map(
//                outboundDetailDO ->{
//                    WmsOutboundOrderDetailVO vo = new WmsOutboundOrderDetailVO();
//                    BeanUtils.copyProperties(outboundDetailDO,vo);
//                    return vo;
//                }
//        ).collect(Collectors.toList());
        List<String> orderCodesList = new ArrayList<>();
        orderCodesList.add(orderCode);
        // 获取详情单VO对象
        List<WmsOutboundOrderDetailVO> detailVOList = outboundOrderDetailService.listDetailVO(orderCodesList);


        // 封装结果
        WmsOutboundOrderAndDetailVO res = new WmsOutboundOrderAndDetailVO(outboundOrderVO,detailVOList);
        return res;
    }

    /**
     * 通过审核单id获取绑定的出库单详情（先获取主单再获取详情单）
     * @param id 审核单id
     * @return 详情List
     * @since 2024/2/13
     */
    @Override
    public List<WmsOutboundOrderDetailDO> findOutboundDetailListByAuditId(Long id) {
        WmsOutboundOrderDO outbound = outboundOrderMapper.selectOne(new QueryWrapper<WmsOutboundOrderDO>().eq("audit_id", id));
        if (outbound == null) {
            return null;
        }
        List<WmsOutboundOrderDetailDO> detailList = outboundOrderDetailMapper.selectList(
                new QueryWrapper<WmsOutboundOrderDetailDO>().eq("outbound_order_code", outbound.getOrderCode()));
        return Optional.ofNullable(detailList).orElse(new ArrayList<>());
    }

    /**
     * 根据出库单号列表获取出库单信息
     * @param orderCodes 出库单号列表
     * @return 出库单信息列表
     */
    @Override
    public List<WmsOutboundOrderDO> selectOutboundOrderListByOrderCodes(List<String> orderCodes) {
        if (orderCodes.isEmpty()) {
            return new ArrayList<>();
        }
        //        List<WmsOutboundOrderDO> wmsOutboundOrderDOS = outboundOrderMapper.selectOutboundOrderListByOrderCodes(orderCodes)
//        List<WmsOutboundOrderDO> wmsOutboundOrderDOS =
//                orderCodes.stream().map(
//                        orderCode -> outboundOrderMapper.selectOutboundOrderListByOrderCodes(orderCode)
//                ).collect(Collectors.toList());
        return orderCodes.stream().map(
                orderCode -> outboundOrderMapper.selectOutboundOrderListByOrderCodes(orderCode)
        ).collect(Collectors.toList());
    }
}
